# Weighted data
library(ggplot2)

# No weighted variable
ggplot(midwest, aes(percwhite, percbelowpoverty)) + geom_point()
# qplot(percwhite, percbelowpoverty, data = midwest)

# Population as weighted variable
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point(aes(size = poptotal / 1e6)) +
  scale_size_area("Population\n(millions)", breaks = c(0.5, 1, 2, 4))
# qplot(percwhite, percbelowpoverty, data = midwest, size = poptotal / 1e6) +
#   scale_size_area("Population\n(millions)", breaks = c(0.5, 1, 2, 4))

# Area as weighted variable
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point(aes(size = area)) + scale_size_area()
# qplot(percwhite, percbelowpoverty, data = midwest, size = area) +
#   scale_size_area()

lm_smooth <- geom_smooth(method = lm, size = 1)
qplot(percwhite, percbelowpoverty, data = midwest) + lm_smooth

# Population density (population / area) as weighted variable
qplot(percwhite, percbelowpoverty, data = midwest,
      weight = poptotal/area, size = poptotal/area) + lm_smooth

qplot(percbelowpoverty, data = midwest, binwidth = 1)
qplot(percbelowpoverty, data = midwest, binwidth = 1, weight = poptotal) +
  ylab("Population")
